package com.springboot.demo.web;

import com.springboot.demo.entity.Book;
import com.springboot.demo.entity.BorrowMsg;
import com.springboot.demo.entity.Reader;
import com.springboot.demo.service.ReaderBRService;
import com.springboot.demo.service.ReaderService;
import com.springboot.demo.service.SearchBookService;
import com.springboot.demo.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.MessagingException;
import javax.servlet.http.HttpSession;
import javax.xml.ws.RequestWrapper;
import java.text.ParseException;
import java.util.Calendar;
import java.util.List;

@Controller
@RequestMapping("/sendpassword")

public class SendMailController {

    @Autowired
    private SendMailService sendMailService;
    @Autowired
    private ReaderService readerService;
    @Autowired
    private ReaderBRService readerBRService;
    @Autowired
    private SearchBookService searchBookService;

    @Value("691258746@qq.com")
    public String from;


    @RequestMapping("/findpassword")
    public String findpassword() {
        return "reader_findpassword";
    }

    @RequestMapping(value="/sendCode",method = RequestMethod.POST)
    @ResponseBody
    public String sendCode(String phone, String email, HttpSession session) throws MessagingException {
        StringBuilder sb = new StringBuilder();
        String code = getCode(phone, email);
        if(code.equals("error1")){
            return "no1";
        }
        if(code.equals("error2")){
            return "no2";
        }
        session.setAttribute("code", code);
        sb.append("<html><head></head>");
        sb.append("<body><h1>Your password is :" + code);
        sb.append("</h1><p>");
        sb.append("</p></body>");
        sb.append("</html>");
        sendMailService.sendHtmlMail(from, email, sb.toString());
        return "success";
    }

    public String getCode(String phone, String email) {
        System.out.println("phone:"+phone);
        System.out.println("email:"+email);
        Reader builder=readerService.getReaderByPhone(phone);
        System.out.println("builder:"+builder);
        if(builder != null && builder.getEmail().equals(email)){
            String code = builder.getPassword();
            System.out.println(code);
            return code;
        }else if(builder != null) {
            return "error1";
        }else{
            return "error2";
        }
    }

    @RequestMapping(value="/searchBookOverdue",method = RequestMethod.POST)
    @ResponseBody
    public String sendBookOverdue(String phone, HttpSession session) throws MessagingException, ParseException {
        StringBuilder sb = new StringBuilder();
        List<BorrowMsg> book = getBook(phone);
        String email = getEmail(phone);
        if(book.isEmpty()){
            return "no1";
        }
        session.setAttribute("book", book);
        sb.append("<html><head></head>");
        sb.append("<body><h1>The books you should return:</h1><h2>");
        for (int i = 0; i < book.size() ; i++) {
            String b = searchBookService.getBookByID(book.get(i).getID()).getBname();
            sb.append((i+1) + ": "+ b + "</h2><h2>");
        }
        sb.append("</h2><p>");
        sb.append("</p></body>");
        sb.append("</html>");
        sendMailService.sendHtmlMailBook(from, email, sb.toString());
        return "success";
    }

    public String getEmail(String phone) {
        System.out.println("phone:" + phone);
        Reader builder = readerService.getReaderByPhone(phone);
        System.out.println("builder:" + builder);
            String email = builder.getEmail();
            System.out.println(email);
            return email;
    }

    public List<BorrowMsg> getBook(String phone) throws ParseException {
        System.out.println("phone:" + phone);
        Calendar calendarOld = Calendar.getInstance();
        Calendar calendarNow = Calendar.getInstance();
        List<BorrowMsg> builder = readerService.getBook(phone);
//        List<BorrowMsg> book = readerService.getBook(phone);
        for (int i = 0; i < builder.size() ; i++) {
            calendarOld=readerBRService.StringToTime(builder.get(i).getStarttime());
            if (readerBRService.TimeMinusTimeDay(calendarOld,calendarNow)<30) {
                builder.remove(i);
                i--;
            }
        }
        return builder;
    }
}
